<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Auth;
use DB;
use Session;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
	
	public function __construct(){
		
	}
	
	
	/*
	* 	操作提示 
	* 	参数：$show_info 你应该输入的内容
	* 		  $url 		 接下来要跳转到哪
	*	登录比较特殊	 参数为两个
	*/
	public function show_msg( $show_info='',$url='' ){
		$return = array();
		$return['show_info']=$show_info;
		$return['url']=$url;
		exit( json_encode( $return ) );
	}
	
	
	/*
	* 	写日志 
	* 	参数：$type		类型 1是资讯
	*		  $link_id	关联表的ID
	*		  $content	操作说明
	*/
	public function write_log( $type=1,$link_id='',$content='' ){
		$data=array();
		$data['type']=$type;
		$data['link_id']=$link_id;
		$data['content']=$content;
		$data['admin_id']=Session::get('admin_id');
		$data['time']=date("Y-m-d H:i:s",time());
		$log_id = DB::table('log')->insertGetId($data);
	}
	
	/*
	* 	读取日志 
	* 	参数：$type		类型 1是资讯
	*		  $link_id	关联表的ID
	*/
	public function read_log( $type=1,$link_id='' ){
		// 返回查询的全部数据
		$log_info = DB::table('log')
						->where( 'type','=',$type )
						->where( 'link_id','=',intval($link_id) )
						->get();
		return $log_info;
	}
	
	/*
	* 	读取全部日志 
	*/
	public function read_All_Log(){
		// 返回查询的全部数据
		$log_info = DB::table('log as l')
						->select('l.*','a.name')
						->leftJoin('admin as a','a.id','=','l.admin_id')
						->orderBy('l.time','desc')
						// 暂取10条
						->take(10)
						->get();
		return $log_info;
	}
	
	
	/*
	* 	查询后台用户名称
	* 	参数：$admin_id 用户id
	*/
	public function getAdminName( $admin_id='' ){
		// 只返回一条数据
		$admin_info = DB::table('admin')
						->where( 'id','=',$admin_id )
						->first();
		return $admin_info;
	}
	
	/*
	* 	查询前台用户名称
	* 	参数：$user_id 用户id
	*/
	public function getUserName( $user_id='' ){
		// 只返回一条数据
		$user_info = DB::table('user')
						->where( 'id','=',$user_id )
						->first();
		return $user_info;
	}
	
	
	
}
